﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SisLegal.Model;
using System.Data.SqlClient;

namespace SisLegal.Maestros
{
    public partial class Users : System.Web.UI.Page
    {
        SisLegal.Model.Utilitarios util = new SisLegal.Model.Utilitarios();
        SqlConnection CNx = new SqlConnection();
        SqlConnection CNx2 = new SqlConnection();

        protected void Page_Load(object sender, EventArgs e)
        {
            //CNx.ConnectionString = Utilitarios.Conexion2;
            //CNx2.ConnectionString = Utilitarios.Conexion;
            CNx.ConnectionString = Util.ConnKLegal;
            CNx2.ConnectionString = Util.CnnBDSystem;

            if (Session["IDUsuario"] == null)
            {
                //Response.Redirect("Default.aspx");
            };

            if (!IsPostBack)
            {
                //cargardatos();
                CargarUsuarios();
                //llenararbol();
            }
        }

        protected void gvUsuarios_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowView = (DataRowView)e.Row.DataItem;
                string IDU = rowView["IDU"].ToString();
                int nivel = (int)rowView["SEG_1"];
                //DateTime F2 = (DateTime)rowView["FechaFin"];
                ImageButton btn1 = (ImageButton)e.Row.FindControl("btnEdit");
                System.Web.UI.WebControls.Image Img1 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img1");
                System.Web.UI.WebControls.Image Img2 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img2");
                System.Web.UI.WebControls.Image Img3 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img3");
                System.Web.UI.WebControls.Image Img4 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img4");
                System.Web.UI.WebControls.Image Img5 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img5");
                System.Web.UI.WebControls.Image Img6 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img6");
                System.Web.UI.WebControls.Image Img7 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img7");
                System.Web.UI.WebControls.Image Img8 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img8");
                System.Web.UI.WebControls.Image Img9 = (System.Web.UI.WebControls.Image)e.Row.FindControl("img9");
                btn1.Attributes.Add("OnClick", "return verUser(" + IDU + ");");
                bool[] rr = new bool[10];
                rr = util.leve(nivel);
                if (rr[1]) Img1.ImageUrl = "/public/img/check.png";
                if (rr[2]) Img2.ImageUrl = "/public/img/check.png";
                if (rr[3]) Img3.ImageUrl = "/public/img/check.png";
                if (rr[4]) Img4.ImageUrl = "/public/img/check.png";
                if (rr[5]) Img5.ImageUrl = "/public/img/check.png";
                if (rr[6]) Img6.ImageUrl = "/public/img/check.png";
                if (rr[7]) Img7.ImageUrl = "/public/img/check.png";
                if (rr[8]) Img8.ImageUrl = "/public/img/check.png";
                if (rr[9]) Img9.ImageUrl = "/public/img/check.png";
            }
        }

        protected void btnVUsers_Click(object sender, EventArgs e)
        {
            int valor = 0;
            int recep = 0;
            if (chk1.Checked) valor = valor + 1;
            if (chk2.Checked) valor = valor + 2;
            if (chk3.Checked) valor = valor + 4;
            if (chk4.Checked) valor = valor + 8;
            if (chk5.Checked) valor = valor + 16;
            if (chk6.Checked) valor = valor + 32;
            if (chk7.Checked) { valor = valor + 64; recep = 1; }
            if (chk8.Checked) valor = valor + 128;
            if (chk9.Checked) valor = valor + 256;
            CNx2.Open();
            string msg = "Update SIS_USERAPP Set SEG_1 = " + valor.ToString() + ",SEG_2=" + recep.ToString() + " where USR_ID = " + hid10.Value;
            SqlCommand tb = new SqlCommand(msg, CNx2);
            tb.ExecuteNonQuery();
            CNx2.Close();
            CargarUsuarios();
        }

        private void CargarUsuarios()
        {
            CNx2.Open();
            string msg = "Select * from SIS_VUAPP where APP_ID=7 order by Usuario";
            DataTable dt = new DataTable();
            SqlDataAdapter Da = new SqlDataAdapter(msg, CNx2);
            Da.Fill(dt);
            gvUsuarios.DataSource = dt;
            gvUsuarios.DataBind();
            CNx2.Close();
        }

        protected void btnUpUsr_Click(object sender, EventArgs e)
        {
            CNx2.Open();
            string msg = "Select * from SIS_VUAPP where APP_ID = 7 and IDU =" + hid10.Value;
            SqlCommand tb = new SqlCommand(msg, CNx2);
            SqlDataReader Dr = tb.ExecuteReader();
            Dr.Read();
            lblUsuario.Text = Dr["Usuario"].ToString();
            lblNUsuario.Text = Dr["Nombres"].ToString();
            int lev = (int)Dr["SEG_1"];
            Dr.Close();

            bool[] level = new bool[10];
            bool bucle = true;
            while (bucle)
            {
                if (lev >= 256) { level[9] = true; chk9.Checked = true; lev = lev - 256; } else chk9.Checked = false;
                if (lev >= 128) { level[8] = true; chk8.Checked = true; lev = lev - 128; } else chk8.Checked = false;
                if (lev >= 64) { level[7] = true; chk7.Checked = true; lev = lev - 64; } else chk7.Checked = false;
                if (lev >= 32) { level[6] = true; chk6.Checked = true; lev = lev - 32; } else chk6.Checked = false;
                if (lev >= 16) { level[5] = true; chk5.Checked = true; lev = lev - 16; } else chk5.Checked = false;
                if (lev >= 8) { level[4] = true; chk4.Checked = true; lev = lev - 8; } else chk4.Checked = false;
                if (lev >= 4) { level[3] = true; chk3.Checked = true; lev = lev - 4; } else chk3.Checked = false;
                if (lev >= 2) { level[2] = true; chk2.Checked = true; lev = lev - 2; } else chk2.Checked = false;
                if (lev >= 1) { level[1] = true; chk1.Checked = true; lev = lev - 1; } else chk1.Checked = false;
                if (lev <= 0) bucle = false;
            }
            CNx2.Close();
        }

    }
}